﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace XFSale.Aux
{
    public partial class DictPage : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtItemName.Text = Request["itemName"];
                if (txtItemName.Text == null)
                    txtItemName.Text = string.Empty;

                dsDict.SelectParameters.Clear();
                dsDict.SelectParameters.Add("itemName", "%" + txtItemName.Text.ToUpper() + "%");
            }
        }

        protected void gvDict_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView row = e.Row.DataItem as DataRowView;
                ImageButton btn = e.Row.FindControl("btnSelect") as ImageButton;
                btn.Visible = Convert.ToString(row["itemDetail"]) == "Y";
            }
        }

        protected void gvDict_RowCommand(object sender, GridViewCommandEventArgs e)
        {
        }

        protected void gvDict_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            e.Cancel = true;

            GridViewRow row = gvDict.Rows[e.NewSelectedIndex];
            Response.Redirect("~/DictValue.aspx?itemName=" + row.Cells[0].Text, true);
        }

        protected void gvDict_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void gvDict_RowEditing(object sender, GridViewEditEventArgs e)
        {
            e.Cancel = true;
            gvDict.Visible = false;
            pnlSearch.Visible = false;

            fvDict.Visible = true;
            fvDict.PageIndex = gvDict.PageIndex * gvDict.PageSize + e.NewEditIndex;
            fvDict.ChangeMode(FormViewMode.Edit);
        }

        protected void gvDict_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

        }

        protected void gvDict_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {

        }

        protected void gvDict_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            dsDictTemp.SelectCommand = "select count(*) from XmDictValue where itemName = :itemName";
            dsDictTemp.SelectParameters.Clear();
            dsDictTemp.SelectParameters.Add("itemName", Convert.ToString(e.Keys["itemName"]));
            IEnumerator em = dsDictTemp.Select(new DataSourceSelectArguments()).GetEnumerator();
            em.MoveNext();
            DataRowView row = em.Current as DataRowView;
            int count = Convert.ToInt32(row[0]);
            if (count > 0)
            {
                e.Cancel = true;
                AlertError("数据项存在取值，请删除所有取值后再删除数据项");
                return;
            }
        }

        protected void gvDict_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {

        }

        protected void fvDict_PreRender(object sender, EventArgs e)
        {
            Button btnCtrl = null;
            TextBox txtCtrl = null;
            DropDownList ddlCtrl = null;
            RadioButtonList rblCtrl = null;
            DataRowView row = fvDict.DataItem as DataRowView;
            switch (fvDict.CurrentMode)
            {
                case FormViewMode.ReadOnly:
                    ChangeToShow();
                    break;

                case FormViewMode.Edit:
                    btnCtrl = fvDict.FindControl("btnInsert") as Button;
                    if (btnCtrl != null)
                    {
                        btnCtrl.Visible = false;
                    }

                    txtCtrl = fvDict.FindControl("txtItemName") as TextBox;
                    txtCtrl.Enabled = false;

                    if (fvDict.DataItem != null)
                    {
                        ddlCtrl = fvDict.FindControl("ddlItemType") as DropDownList;
                        ddlCtrl.SelectedValue = Convert.ToString(row["itemType"]);

                        rblCtrl = fvDict.FindControl("rblItemDetail") as RadioButtonList;
                        rblCtrl.SelectedValue = Convert.ToString(row["itemDetail"]);
                    }

                    break;

                case FormViewMode.Insert:
                    btnCtrl = fvDict.FindControl("btnUpdate") as Button;
                    if (btnCtrl != null)
                    {
                        btnCtrl.Visible = false;
                    }

                    txtCtrl = fvDict.FindControl("txtItemName") as TextBox;
                    txtCtrl.Enabled = true;

                    break;
            }
        }

        protected void fvDict_ItemCommand(object sender, FormViewCommandEventArgs e)
        {
            if (e.CommandName.ToUpper() == "Cancel".ToUpper())
            {
                ChangeToShow();
            }
        }

        protected void fvDict_ItemUpdating(object sender, FormViewUpdateEventArgs e)
        {
            DropDownList ddlItemType = fvDict.FindControl("ddlItemType") as DropDownList;
            RadioButtonList rblItemDetail = fvDict.FindControl("rblItemDetail") as RadioButtonList;

            if (rblItemDetail.SelectedValue == "N")
            {
                dsDictTemp.SelectCommand = "select count(*) from XmDictValue where itemName = :itemName";
                dsDictTemp.SelectParameters.Clear();
                dsDictTemp.SelectParameters.Add("itemName", Convert.ToString(e.Keys["itemName"]));
                IEnumerator em = dsDictTemp.Select(new DataSourceSelectArguments()).GetEnumerator();
                em.MoveNext();
                DataRowView row = em.Current as DataRowView;
                int count = Convert.ToInt32(row[0]);
                if (count > 0)
                {
                    e.Cancel = true;
                    AlertError("数据项存在取值，允许取值不可以修改为否");
                    return;
                }
            }

            e.NewValues["itemType"] = ddlItemType.SelectedValue;
            e.NewValues["itemDetail"] = rblItemDetail.SelectedValue;
        }

        protected void fvDict_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
        {
            ChangeToShow();
        }

        protected void fvDict_ItemInserting(object sender, FormViewInsertEventArgs e)
        {
            dsDictTemp.SelectCommand = "select count(*) from XmDict where upper(itemName) = upper(:itemName)";
            dsDictTemp.SelectParameters.Clear();
            dsDictTemp.SelectParameters.Add("itemName", Convert.ToString(e.Values["itemName"]));
            IEnumerator em = dsDictTemp.Select(new DataSourceSelectArguments()).GetEnumerator();
            em.MoveNext();
            DataRowView row = em.Current as DataRowView;
            int count = Convert.ToInt32(row[0]);
            if (count > 0)
            {
                e.Cancel = true;
                AlertError("数据项名称重复");
                return;
            }

            DropDownList ddlItemType = fvDict.FindControl("ddlItemType") as DropDownList;
            e.Values["itemType"] = ddlItemType.SelectedValue;

            RadioButtonList rblItemDetail = fvDict.FindControl("rblItemDetail") as RadioButtonList;
            e.Values["itemDetail"] = rblItemDetail.SelectedValue;
        }

        protected void fvDict_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            ChangeToShow();
        }

        protected void btnSearch_Click(object sender, ImageClickEventArgs e)
        {
            dsDict.SelectParameters.Clear();
            dsDict.SelectParameters.Add("itemName", "%" + txtItemName.Text.ToUpper() + "%");
            gvDict.DataBind();
        }

        protected void btnInsert_Click(object sender, ImageClickEventArgs e)
        {
            gvDict.Visible = false;
            pnlSearch.Visible = false;

            fvDict.Visible = true;
            fvDict.InsertItemTemplate = fvDict.EditItemTemplate;
            fvDict.ChangeMode(FormViewMode.Insert);
        }

        private void ChangeToShow()
        {
            gvDict.Visible = true;
            pnlSearch.Visible = true;
            fvDict.Visible = false;
        }
    }
}
